package protect.week04;

/**
 * @author thinkpad - 83start
 * @version v1.0
 * @create 2022/9/23 10:00
 * @package protect.week04
 * @description 文件说明
 */

public class Ch02_DayOfYear {
    public int dayOfYear(String date) {
        String[] split = date.split("-");
        int year =  Integer.parseInt(split[0]);
        int month =  Integer.parseInt(split[1]);
        int day =  Integer.parseInt(split[2]);

        int days = 0;

        if (month ==  1){
            days = day;
        }else {

            for (int i = 0; i < month; i++) {
                days += getMonthDay(year,i);
            }
            days += day;
        }

        return days;
    }

    int getMonthDay(int year , int month){
        int[] mdays = {0, 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

        // 判断是否是 2 月
        if (month == 2){

            if ((year % 400) == 0 || ((year % 4) == 0) && ((year % 100) != 0)) { // 如果是闰年
                return 29;
            }else { // 如果不是闰年
                return 28;
            }
        }else {
            return mdays[month];
        }
    }


    public static void main(String[] args) {
        Ch02_DayOfYear ch02_dayOfYear = new Ch02_DayOfYear();
        System.out.println(ch02_dayOfYear.dayOfYear("2003-03-01"));
    }
}
